
//
//  MyTableViewOfMoment.m
//  Beauty
//
//  Created by Snow on 16/9/7.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "MyTableViewOfMoment.h"
#import "MyMomentOfCollectionViewCell.h"
#import "GXMacro.h"
#import "MyWindowCollectionViewCell.h"
#import "MineModel.h"
@interface MyTableViewOfMoment ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@end
@implementation MyTableViewOfMoment
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _layout = [[UICollectionViewFlowLayout alloc] init];
        _layout.minimumLineSpacing = 10;
        _layout.minimumInteritemSpacing = 10;
        _layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout];
        [self.contentView addSubview:_collectionView];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.scrollEnabled = NO;
        _collectionView.backgroundColor = [UIColor whiteColor];
        [_collectionView registerClass:[MyMomentOfCollectionViewCell class] forCellWithReuseIdentifier:@"MyMomentOfCollectionViewCell"];
        [_collectionView registerClass:[MyWindowCollectionViewCell class] forCellWithReuseIdentifier:@"MyWindowCollectionViewCell"];
    }
    return self;
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    _layout.itemSize = CGSizeMake((self.contentView.bounds.size.width - 40) / 3, 120);
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView.mas_top);
        make.left.mas_equalTo(self.contentView.mas_left);
        make.right.mas_equalTo(self.contentView.mas_right);
        make.bottom.mas_equalTo(self.contentView.mas_bottom);
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _arrayOfMoment.count + 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        MyMomentOfCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyMomentOfCollectionViewCell" forIndexPath:indexPath];
        return cell;
    } else {
    MyMomentOfCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyMomentOfCollectionViewCell" forIndexPath:indexPath];
        cell.backgroundColor = [UIColor greenColor];
        MineModel *model = [_arrayOfMoment objectAtIndex:indexPath.row - 1];
        [cell.image sd_setImageWithURL:[NSURL URLWithString:model.commend_image]];
    return cell;
    }
}
- (void)setArrayOfMoment:(NSArray<MineModel *> *)arrayOfMoment
{
    if (arrayOfMoment.count != 0) {
        
        _arrayOfMoment = arrayOfMoment;
    }
    [_collectionView reloadData];
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row != 0) {
        MineModel *model = [_arrayOfMoment objectAtIndex:indexPath.row - 1];
        if ([self.delegate respondsToSelector:@selector(momentWithPersonID:)] && self.delegate != nil) {
            [self.delegate momentWithPersonID:model.personal_id];
        }
    }
}
@end
